Log4js = require("log4js")

const RootPath = ( global.LogPath || process.env.HOME )

Log4js.configure({
    appenders: {
        console : {
            type: "console" ,
            level : "ALL"
        },
        file : {
            type: "file",
            level: 'INFO',
            filename: RootPath + "/out",
            alwaysIncludePattern: true,
            daysToKeep: 2,
            pattern:  "yyyy-MM-dd-hh.log",
            encoding: 'utf-8'
        },
        errorout : {
            type: "dateFile",
            level: 'ERROR',
            filename: RootPath + "/error",
            alwaysIncludePattern: true,
            daysToKeep: 2,
            pattern:  "yyyy-MM-dd.log",
            encoding: 'utf-8'
        }
    },
    categories: {
        default: {
            appenders: ['console', 'file' ] , level : "ALL"
        },//默认log类型，输出到控制台 log文件 log日期文件 且登记大于info即可
        file: { 
            appenders: ['file'], level: 'INFO' 
        },  //生产环境 log类型 只输出到按日期命名的文件，且只输出警告以上的log
        console: { 
            appenders: ['console'], level: 'DEBUG' 
        }, //开发环境  输出到控制台
        debug: { 
            appenders: ['console'], level: 'DEBUG' 
        }, //调试环境 输出到log文件和控制台    
        err: { 
            appenders: ['errorout'], level: 'ERROR' 
        }//error 等级log 单独输出到error文件中 任何环境的errorlog 将都以日期文件单独记录
    },
})

module.exports = {
    configure( opt ) {
        if( opt ) {
            Log4js.configure ( opt )
        }
        return this
    } ,
    init( ) {
        //保证同步启动
        global.Log = Log4js.getLogger()
        global.GetLogger = Log4js.getLogger
        global.LogConfigure = Log4js.configure
    }

}